package com.wuchao.hbaselearn;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.client.TableDescriptor;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

/**
 * 作者：吴超
 * 邮箱：wuchao_19931120@163.com
 * 时间：2021-12-22
 * 描述：
 */

public class HBaseDemo {

    private Connection connection;
    private Admin admin;

    /**
     *  @author 吴超
     *  @time 2021/12/22 0:46
     *  通过配置拿到连接，并拿到admin，因为DDL操作都需要admin的权限
     */
    @Before
    public void init() throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
        connection = ConnectionFactory.createConnection(configuration);
        admin = connection.getAdmin();
    }


    /**
     *  @author 吴超
     *  @time 2021/12/22 0:46
     *  关闭连接
     */
    @After
    public void close() throws IOException {
        if(admin != null) admin.close();
        if(connection != null) connection.close();
    }


    /**
     *  @author 吴超
     *  @time 2021/12/22 0:24
     *  判断表是否存在
     */

    @Test
    public void isTableExist() throws IOException {
        System.out.println(admin.tableExists(TableName.valueOf("person")));
    }

    /**
     *  @author 吴超
     *  @time 2021/12/22 0:37
     *  创建命名空间
     */
    @Test
    public  void createNameSpace() throws IOException {

        NamespaceDescriptor nmsp = NamespaceDescriptor.create("bigdata").build();
        admin.createNamespace(nmsp);

    }

    /**
     *  @author 吴超
     *  @time 2021/12/22 0:49
     *  查看命名空间
     */
    @Test
    public void listNameSpace() throws IOException {
        NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
        for (NamespaceDescriptor desc : namespaceDescriptors) {
            System.out.println(desc.getName());
        }

    }


    /**
     *  @author 吴超
     *  @time 2021/12/22 0:57
     *  获取命名空间下的表
     */
    @Test
    public void listNameSpaceTables() throws IOException {
        List<TableDescriptor> tableDescriptors = admin.listTableDescriptorsByNamespace("bigdata".getBytes());
        for (TableDescriptor desc : tableDescriptors) {
            String tblName = new String(desc.getTableName().getName());
            System.out.println(tblName);
        }
    }

    /**
     *  @author 吴超
     *  @time 2021/12/22 1:03
     *  删除命名空间
     *  1. 先要删除命名空间下的所有表
     *      1.1 先要禁用表
     *      1.2 删除表
     *  2. 删除命名空间
     */
    @Test
    public void dropNameSpace() throws IOException {
        List<TableDescriptor> tableDescriptors = admin.listTableDescriptorsByNamespace("bigdata".getBytes());
        for (TableDescriptor desc : tableDescriptors) {
            // 禁用表
            admin.disableTable(desc.getTableName());
            System.out.println("禁用表： " + new String(desc.getTableName().getName()));
            admin.deleteTable(desc.getTableName());
            System.out.println("删除表： " + new String(desc.getTableName().getName()));
            admin.deleteNamespace("bigdata");
            System.out.println("删除命名空间！");
        }
    }



}
